package com.ruoyi.syrw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.stream.StreamUtil;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.domain.BaReportGroupDetail;
import com.ruoyi.syrw.domain.vo.BaReportGroupDetailVo;
import com.ruoyi.syrw.mapper.BaReportGroupDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.syrw.domain.bo.BsReportGroupBo;
import com.ruoyi.syrw.domain.vo.BsReportGroupVo;
import com.ruoyi.syrw.domain.BsReportGroup;
import com.ruoyi.syrw.mapper.BsReportGroupMapper;
import com.ruoyi.syrw.service.IBsReportGroupService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 试验任务预选分组主Service业务层处理
 *
 * @author zyb
 * @date 2024-06-06
 */
@RequiredArgsConstructor
@Service
public class BsReportGroupServiceImpl implements IBsReportGroupService {

    private final BsReportGroupMapper baseMapper;
    private final BaReportGroupDetailMapper detailMapper;

    /**
     * 查询试验任务预选分组主
     */
    @Override
    public BsReportGroupVo queryById(Long id){
        BsReportGroupVo bsReportGroupVo = baseMapper.selectVoById(id);
        // 查询详细
        List<Long> excelTypeList = StreamUtil.of(detailMapper.selectVoList(new LambdaQueryWrapper<BaReportGroupDetail>()
            .eq(BaReportGroupDetail::getBsId, id)))
            .map(BaReportGroupDetailVo::getExcelTypeId).collect(Collectors.toList());
        bsReportGroupVo.setExcelTypeIdList(excelTypeList);
        return bsReportGroupVo;
    }

    /**
     * 查询试验任务预选分组主列表
     */
    @Override
    public TableDataInfo<BsReportGroupVo> queryPageList(BsReportGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsReportGroup> lqw = buildQueryWrapper(bo);
        Page<BsReportGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(v->{
            List<Long> excelTypeList = StreamUtil.of(detailMapper.selectVoList(new LambdaQueryWrapper<BaReportGroupDetail>()
                .eq(BaReportGroupDetail::getBsId, v.getId())))
                .map(BaReportGroupDetailVo::getExcelTypeId).collect(Collectors.toList());
            v.setExcelTypeIdList(excelTypeList);
        });

        return TableDataInfo.build(result);
    }

    /**
     * 查询试验任务预选分组主列表
     */
    @Override
    public List<BsReportGroupVo> queryList(BsReportGroupBo bo) {
        LambdaQueryWrapper<BsReportGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsReportGroup> buildQueryWrapper(BsReportGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsReportGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, BsReportGroup::getUserId, bo.getUserId());
        lqw.eq(bo.getDeptId() != null, BsReportGroup::getDeptId, bo.getDeptId());
        lqw.eq(bo.getSyTypeId() != null, BsReportGroup::getSyTypeId, bo.getSyTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getGroupName()), BsReportGroup::getGroupName, bo.getGroupName());
        return lqw;
    }

    /**
     * 新增试验任务预选分组主
     */
    @Override
    @Transactional
    public Boolean insertByBo(BsReportGroupBo bo) {
        BsReportGroup add = BeanUtil.toBean(bo, BsReportGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            // 详细
            if (CollUtil.isNotEmpty(bo.getExcelTypeIdList())) {
                bo.getExcelTypeIdList().forEach(v->{
                    detailMapper.insert(new BaReportGroupDetail(add.getId(), v));
                });
            }
        }
        return flag;
    }

    /**
     * 修改试验任务预选分组主
     */
    @Override
    @Transactional
    public Boolean updateByBo(BsReportGroupBo bo) {
        BsReportGroup update = BeanUtil.toBean(bo, BsReportGroup.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            // 详细
            if (ObjUtil.isNotNull(bo.getExcelTypeIdList())) {
                // 先删后加
                detailMapper.delete(new LambdaQueryWrapper<BaReportGroupDetail>()
                    .eq(BaReportGroupDetail::getBsId, bo.getId()));
                bo.getExcelTypeIdList().forEach(v -> {
                    detailMapper.insert(new BaReportGroupDetail(bo.getId(), v));
                });
            }
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsReportGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试验任务预选分组主
     */
    @Override
    @Transactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        boolean flag = baseMapper.deleteBatchIds(ids) > 0;
        if (flag) {
            detailMapper.delete(new LambdaQueryWrapper<BaReportGroupDetail>()
                .in(BaReportGroupDetail::getBsId, ids));
        }
        return flag;
    }
}
